package com.buyhouse.zhaimao.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.model.CommunityBean;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class CommunityFragmentAdapter  extends BaseAdapter{
	private Context context;
	private List<CommunityBean> listCommunityBean;
	private LayoutInflater inflater;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options, headOptions;
	
	public CommunityFragmentAdapter(Context context, List<CommunityBean> listCommunityBean){
		this.context = context;
		this.listCommunityBean = listCommunityBean;
		this.inflater=LayoutInflater.from(context);
		initImgLoader(context);
	}
	
	public ImageLoader getImageLoader() {
		return imageLoader;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listCommunityBean.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listCommunityBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if (convertView == null) {
			 convertView = inflater.inflate(R.layout.item_community, null);
			holder=new ViewHolder();
                                                                         
            holder.community_imgUrl=(ImageView) convertView.findViewById(R.id.community_imgUrl);
            holder.community_name=(TextView) convertView.findViewById(R.id.community_name);
            
            convertView.setTag(holder);
        }else{
        	holder=(ViewHolder) convertView.getTag();
        }

             
		imageLoader.displayImage(listCommunityBean.get(position).getImgUrl(), holder.community_imgUrl,
				options);
		holder.community_name.setText(listCommunityBean.get(position).getName());
               
        return convertView;
	}
	
	class ViewHolder{
		ImageView community_imgUrl;
		TextView expertCount;
		TextView houseCount;
		TextView community_name;
		TextView favorite;
	}

	
	private void initImgLoader(Context context) {
		imageLoader
				.init(new ImageLoaderConfiguration.Builder(context)
						.threadPriority(Thread.NORM_PRIORITY - 2)
						.threadPoolSize(3)
						.denyCacheImageMultipleSizesInMemory()
						.memoryCache(new WeakMemoryCache())
						.diskCacheFileNameGenerator(new Md5FileNameGenerator())
						.tasksProcessingOrder(QueueProcessingType.LIFO)
						.denyCacheImageMultipleSizesInMemory().writeDebugLogs()
						.build());
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.imageScaleType(ImageScaleType.EXACTLY)
				// 图像将完全按比例缩小的目标大小
				.cacheOnDisk(true)
				// 设置下载的图片是否缓存在SD卡中
				.showImageForEmptyUri(R.drawable.ic_home)
				.showImageOnFail(R.drawable.ic_home)
				.showImageOnLoading(R.drawable.ic_home)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		


	}
	
}
